#include "courier.h"
#include "ui_courier.h"

Courier::Courier(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Courier)
{
    ui->setupUi(this);
    this->setFixedSize(440,720);
    ui->pushButton_2->setDisabled(true);

    socket = SocketManager::getinstance().socket();
    qDebug()<<"*****";
    connect(socket,&QAbstractSocket::readyRead,this,&Courier::rcv_rgs);

}

Courier::~Courier()
{
    delete ui;
}

void Courier::on_pushButton_clicked()
{
    emit(returnregister());
}
//用户条例是否确认的判断
void Courier::on_checkBox_stateChanged(int arg1)
{
     if(arg1==0)
     {
         ui->pushButton_2->setDisabled(true);
     }
     else
     {
         ui->pushButton_2->setDisabled(false);
     }
}
//快递员注册功能
void Courier::on_pushButton_2_clicked()
{
    sendRig rig;
    rig.type = 0;//快递员注册标志
    const char * account = ui->lineEdit->text().toUtf8().constData();
    strcpy(rig.Account,account);
    const char * password = ui->lineEdit_2->text().toUtf8().constData();
    strcpy(rig.password,password);
    const char * telenum = ui->lineEdit_3->text().toUtf8().constData();
    strcpy(rig.tele,telenum);
    rig.IC = ui->lineEdit_4->text().toInt();
    rig.Idtype = false;
    QByteArray data;
    data.append((char*)&rig,sizeof (sendRig));
    socket->write(data);
}

void Courier::rcv_rgs()
{
    if(!this->isActiveWindow()){
        return;
    }
    sendRig rd;
    while (socket->bytesAvailable() >= sizeof(sendRig)) {
            socket->read(reinterpret_cast<char*>(&rd), sizeof(sendRig));
    }
//    rd = (sendRig *)socket->readAll().data();
    qDebug()<<"****"<<rd.type;
    if(rd.type == 0){
        if(rd.IC == 1)
        {
            emit(rigfinish());
        }else if(rd.IC == -1)
        {
            //电话号码已注册
            QMessageBox::warning(this,"注册失败","电话号码已注册","返回 ");
        }else{
            //注册失败
            QMessageBox::warning(this,"注册失败","注册失败，请稍后重试","返回 ");
        }
    }
}
